#!/bin/bash

printf "%s" "正在检查网络 IPv4 状况 ...                  "
ip4addr=$(curl --connect-timeout 15 -s ipv4bot.whatismyipaddress.com 2>&1)
if [ $? -eq 0 ]; then
    printf "%10s\n" "[ 正常 ]"
    echo "* 您的 IPv4 地址是：$ip4addr"
else
    printf "%10s\n" "[ 异常 ]"
fi


printf "%s" "正在检查网络 IPv6 状况 ...                  "
ip6addr=$(curl --connect-timeout 15 -s ipv6bot.whatismyipaddress.com 2>&1)
if [ $? -eq 0 ]; then
    printf "%10s\n" "[ 正常 ]"
    echo "* 您的 IPv6 地址是：$ip6addr"
else
    printf "%10s\n" "[ 异常 ]"
fi

echo ""
printf "%s" "正在检查 80 Web 端口 ...                    "
p80=$(netstat -ntlp 2>/dev/null| grep ':80 ') && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
printf "%s" "正在检查 443 安全的 Web 端口 ...            "
p443=$(netstat -ntlp 2>/dev/null| grep ':443 ') && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
printf "%s" "正在检查 10060 http4to6 后台面板 ...        "
p10060=$(netstat -ntlp 2>/dev/null| grep ':10060 ') && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
printf "%s" "正在检查 10060 http4to6 Web 代理 ...        "
p10061=$(netstat -ntlp 2>/dev/null| grep ':10061 ') && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
# printf "%s" "正在检查 9000 Portianer 端口 ...            "
# p9000=$(netstat -ntlp 2>/dev/null| grep ':9000 ') && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
echo "* 检查其他端口请手动执行 netstat -ntlp | grep ':12345 '"


echo ""
read -p "请输入异常网站的域名 (www.abc.com): " domain
read -p "请输入异常网站的端口号 (如 80)    : " portnum
read -p "请输入异常网站的协议 (http/https) : " proto
if [ "$portnum" = "80" ] || [ "$portnum" == 443 ]
then
    portnum=""
else
    portnum=":$portnum"
fi
if [ "$proto" != "http" ] && [ "$proto" != "https" ]
then
    echo "* 不支持的协议 $proto"
    exit 1
fi


echo ""
printf "%s\n" "正在查询 DNS 信息，请人工检查: "
busybox nslookup "$domain" 114.114.114.114
printf "%s" "正在检测源站是否正常 ...                    "
(curl --connect-timeout 15 -sk "$proto://$domain$portnum" >/dev/null 2>&1) && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"
printf "%s" "正在验证经过反向代理是否正常 ...            "
(curl --connect-timeout 15 -sk "$proto://127.0.0.1$portnum" -H "Host: $domain$portnum" >/dev/null 2>&1) && printf "%10s\n" "[ 正常 ]" || printf "%10s\n" "[ 异常 ]"

echo "如果以上所有都正常，请检查系统防火墙和外网连通性是否正常! 具体请咨询网络管理员。"
echo "http4to6 自检脚本 - xRetiaProxyChecker 1.0"

